using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HitBox : MonoBehaviour
{
    private PolygonCollider2D polygonCollider2D;
    private int atk;
    void Start()
    {
        polygonCollider2D = gameObject.GetComponent<PolygonCollider2D>();
        polygonCollider2D.enabled = false;
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void Attack(int atk)
    {
        this.atk = atk;
        StartCoroutine(StartAttack());
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Enemy")
        {
            Role role = other.gameObject.GetComponent<Role>();
            role.Damage(-atk);
        }
    }

    IEnumerator StartAttack()
    {
        yield return new WaitForSeconds(0.2f);
        polygonCollider2D.enabled = true;
        StartCoroutine(EndAttack());
    }

    IEnumerator EndAttack()
    {
        yield return new WaitForSeconds(0.3f);
        polygonCollider2D.enabled = false;
    }
}
